
using EFCore.Context;
using EFCore.Repositories;
using EFCore.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using SqlSugar;

namespace EFCore
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            /*
               CREATE PROCEDURE InsertSeckills(IN start_num INT, IN end_name INT)
               COMMENT '先学习下'
               BEGIN
               declare i int default 0;
               set autocommit = 0;
               set i = start_num;
               WHILE i <= end_name DO
               -- 这里为了示例简单，我们仅使用序号作为名字的一部分
               insert into seckills(Id,SeckillType,SeckillName,SeckillUrl,SeckillPrice,SeckillStock,SeckillPercent,TimeId,ProductId,SeckillLimit,SeckillDescription,SeckillIstop,SeckillStatus) 
               values(i,i,CONCAT('p6+架构班',i),CONCAT('ydt',i),i,i,CONCAT('2021',i),i,i,i,CONCAT('tony',i),i,i);
               SET i = i + 1;
               END WHILE;
               END
               CALL InsertSeckills(2,100)
            */

            /*
                CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertSeckillrecords`(
                	IN `Id` INT,
                	IN `numCount` INT
                )
                    COMMENT '先学习下'
                BEGIN
                declare i int default 0;
                set i = 0;
                WHILE i <= numCount DO
                -- 这里为了示例简单，我们仅使用序号作为名字的一部分
                insert into seckillrecords(Createtime,Updatetime,RecordTotalprice,SeckillId,SeckillNum,SeckillPrice,ProductPrice,OrderSn,UserId,RecordStatus) 
                values(NOW(),NOW(),i,Id,i,i,i,i,i,i);
                SET i = i + 1;
                COMMIT;
                END WHILE;
                END
             */
            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            //builder.Services.AddMemoryCache(option => option.SizeLimit = 4);
            builder.Services.AddDbContextPool<SeckillContext>(option =>
            {
                option.UseMySql("server=127.0.0.1;port=3306;database=EFCore;uid=root;pwd=123456", new MySqlServerVersion("8.1.0-mysql"),
                    o => 
                    {
                        //设置efcore最大批处理次数
                        o.MinBatchSize(1); //更新批处理得最小个数
                        o.MaxBatchSize(4);//更新批处理得最大个数
                    });
                option.UseLoggerFactory(LoggerFactory.Create(x => x.AddConsole()));
                //开启懒加载
                //option.UseLazyLoadingProxies();
            });
            builder.Services.AddTransient(typeof(SqlSugarContext));
            //var xx = LoggerFactory.Create(x => x.AddConsole());
            builder.Services.AddScoped<ISeckillRepository, SeckillRepository>();
            builder.Services.AddScoped<ISeckillService, SeckillService>();
            var app = builder.Build();
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            //var data = app.Services.GetService<IMemoryCache>();
            app.UseAuthorization();
            app.MapControllers();
            app.Run();
        }
    }
}
